package com.xyh.flowershop.aspects;

import com.alibaba.fastjson.JSON;
import com.xyh.flowershop.cache.RedisCache;
import com.xyh.flowershop.constant.CacheConstant;
import com.xyh.flowershop.context.WebContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author 小约翰可汗
 * @className RefreshTokenAspect
 * @createTime 2024/5/620:02
 */
@Slf4j
@Aspect
@Component
public class RefreshTokenAspect {

    private final RedisCache redisCache;

    public RefreshTokenAspect(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    /**
     * 如果需要作用在类上需要加入within
     */
    @Pointcut("@annotation(com.xyh.flowershop.annotations.RefreshToken) || @within(com.xyh.flowershop.annotations.RefreshToken)")
    public void servicePoint(){

    }

    @After("servicePoint()")
    public void doService(JoinPoint joinPoint){
        String key = CacheConstant.LOGIN_PREFIX+ WebContext.get();
        Object cacheObject = redisCache.getCacheObject(key);
        if(!Objects.isNull(cacheObject)){
            redisCache.setCacheObject(key,cacheObject);
        }
    }
}
